<!-- ERP 产品库存列表 -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="name">
        <el-select
          v-model="queryParams.itemType"
          clearable
          filterable
          placeholder="请选择物品类型"
          class="!w-240px"
        >
          <el-option

            label="物料"
            value="0"
          />
          <el-option
            label="产品"
            value="1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          value-format="x"
          placeholder="选择创建时间"
          class="!w-1/1"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"> 搜索</el-button>
        <el-button @click="resetQuery" plain type="primary"> 重置</el-button>
        <el-button @click="btnLie"  type="primary">列表字段</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:stock:create']"
        >
           新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
       
          v-hasPermi="['erp:stock:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table   v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="图片" align="center"  v-if="visibleColumns.image"  prop="productName" />
      <el-table-column label="产品编号" align="center" prop="id" />
      <el-table-column label="产品名称" align="center" v-if="visibleColumns.name"  prop="name" />
      <el-table-column
        label="产品分类"
        align="center"
        v-if="visibleColumns.count" 
        prop="count"
        :formatter="erpCountTableColumnFormatter"
      />
      <el-table-column label="产品规格"  v-if="visibleColumns.feilei"  align="center" prop="feilei" />
      <el-table-column label="产品属性"  v-if="visibleColumns.shuxing" align="center" prop="shuxing" />
      <el-table-column label="产品备注" v-if="visibleColumns.beizhu" align="center" prop="beizhu" />
      <el-table-column label="产品使用状态" v-if="visibleColumns.zhuangtai" align="center" prop="zhuangtai" />
      <el-table-column label="单位(基本单位)" v-if="visibleColumns.danwei" align="center" prop="danwei" />
      <el-table-column label="入库备注" align="center" v-if="visibleColumns.beizhu2" prop="beizhu" />
      <el-table-column label="销售单价(基本单位)" align="center" v-if="visibleColumns.danwei2" prop="danwei2" />
      <el-table-column label="成本单价(基本单位)" align="center" v-if="visibleColumns.danwei3" prop="danwei3" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 自定义字段 -->
  <el-dialog
    v-model="dialogVisible"
    title="自定义字段"
    width="50%"
    :before-close="handleClose"
  >
    <div class="content">
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.image"  label="图片" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.bianhao"  label="产品编号" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.name"  label="产品名称" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.count"  label="产品分类" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.feilei"  label="产品规格" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.shuxing"  label="产品属性" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.beizhu"  label="产品备注" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.zhuangtai"  label="产品使用状态" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei"  label="单位(基本单位)" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.beizhu2"  label="入库备注" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei2"  label="销售单价(基本单位)" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei3"  label="成本单价(基本单位)" size="large" /></div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

</template>

<script setup lang="ts">
import download from '@/utils/download'
import { StockApi, StockVO } from '@/api/erp/stock/stock'
import { ProductApi, ProductVO } from '@/api/erp/product/product'
import { WarehouseApi, WarehouseVO } from '@/api/erp/stock/warehouse'
import { erpCountTableColumnFormatter } from '@/utils'
import * as ContactApi from "@/api/crm/contact";

/** ERP 产品库存列表 */
defineOptions({ name: 'ErpStock' })
const productId=ref("")
const warehouseId=ref("")
const warehouseAreaId=ref("")
const warehouseLocationId=ref("")
const pageNo=ref(1)
const input=ref("")
const pageSize=ref(10)
const dialogVisible=ref(false)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false) // 列表的加载中
const visibleColumns =ref({
  image:true,
  bianhao:true,
  name:true,
  count:true,
  feilei:true,
  shuxing:true,
  beizhu:true,
  zhuangtai:true,
  danwei:true,
  beizhu2:true,
  danwei2:true,
  danwei3:true
})
const list = ref<StockVO[]>([
  {
    name:'产品1',
    bianhao:'121212-dfdf',
    feilei:'A类',
    shuxing:'121212',
    beizhu:'ddfdf',
    zhuangtai:'已使用',
    danwei:'mk',
    cangku:'A-仓库',
    kuqu:'a-12区',
    kuwei:'A-12',
    kucun:'12',
    count:'111',
    danwei2:'kg',
    danwei3:'mc',
    beizhu2:"fdfd"
  }
]) 

// 用于临时存储弹框中的选项
const tempVisibleColumns = ref({...visibleColumns.value})
// 打开弹框时初始化临时数据
const btnLie = () => {
  tempVisibleColumns.value = {...visibleColumns.value}
  dialogVisible.value = true
}
// 确认选择
const handleConfirm = () => {
  visibleColumns.value = {...tempVisibleColumns.value}
  dialogVisible.value = false
  
  // 这里可以添加保存到本地存储的逻辑
  // localStorage.setItem('visibleColumns', JSON.stringify(visibleColumns.value))
}
// 取消选择
const handleCancel = () => {
  dialogVisible.value = false
}








// 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  itemType:"1",
  productId: undefined,
  warehouseId: undefined
})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const productList = ref<ProductVO[]>([]) // 产品列表
const warehouseList = ref<WarehouseVO[]>([]) // 仓库列表

//点击列表字段
// function btnLie(){
//   dialogVisible.value=true
// }
/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const data = await StockApi.getStockPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StockApi.deleteStock(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StockApi.exportStock(queryParams)
    download.excel(data, '产品库存.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  //await getList()
  // 加载产品、仓库列表
  getList()
  // productList.value = await ProductApi.getProductSimpleList()
  // warehouseList.value = await WarehouseApi.getWarehouseSimpleList()
})
</script>
